<?php

namespace backend\models;

use common\helps\tools;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;
use yii\db\Query;

/**
 * This is the model class for table "InformationClass".
 *
 */
class InformationClass extends ActiveRecord
{
    public $type;// 操作类型
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%information_class}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return[
            [['title'], 'trim'],
            [['title','status','type'], 'required', 'message'=>'{attribute}不能为空'],
            [['type','status'], 'match', 'pattern' => '/^[1-9][0-9]*$/','message'=>'{attribute}值无效'],
//            [['title'], 'unique', 'message'=>'{attribute}已存在'],
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '资讯分类id',
            'title' => '名称',
            'info'=>'分类描述',
            'status'=>'资讯分类状态',
            'type'=>'操作类型',
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [
                'id',
                'title',
                'info',
                'status',
                'created_at',
                'updated_at',
                'deleted_at',
                'type',
            ],
            'create' => [
                'title',
                'info',
                'status',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'update'=>[
                'id',
                'title',
                'info',
                'status',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
        ];
    }



    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }


    /**
     * 获取资讯分类列表
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getInformationClassList($params){
        if(!empty($params['title']) && !empty($params['status'])){
            $condition = ['and','status='.trim($params['status']),['like','title',$params['title']],['deleted_at'=>null]];
        }elseif(!empty($params['title'])){
            $condition = ['and',['like','title',$params['title']],['deleted_at'=>null]];
        }elseif(!empty($params['status'])){
            $condition =['and','status='.trim($params['status']),['deleted_at'=>null]];
        }else{
            $condition['deleted_at']=null;
        }
        $list=$this->find()->select('id,title,status,info')->where($condition)->orderBy('id desc')->asArray()->all();
        return empty($list)?array():$list;

    }




    /**
     * 删除于停用项目分类
     * @param $id
     * @param $type
     * @author hey_heyang(635870453@qq.com)
     * @return boolean
     */
    public  function delInformationClass($id,$type){
        $model =  static::findOne(['id' =>$id]);
        if(empty($model)) return false;
        $model->scenario='update';
        if($type==1){ //启用
            $param['status']=1;
            $model->setAttributes($param, false);
            $res = $model->save();

        }elseif($type==2){ // 停用
            $param['status']=2;
            $model->setAttributes($param, false);
            $res = $model->save();
        }elseif($type==3){ //删除
            $res = $model->delete();
        }else{
            return false;
        }
        if($res){
            return true;
        }else{
            return false;
        }

    }


    /**
     * 新增与编辑项目分类
     * @param $param
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function editInformationClass($param){
        if($param['type']>1){ //编辑
            $model = static::findOne(['id' =>$param['id']]);
            if(empty($model)) return array('error'=>'未查询到分类信息');
            $model->scenario = 'update';
        }else{
            $model = new self();
            $model->scenario = 'create';
            unset($param['id']);
        }
        $model->setAttributes($param, false);
        if (!$model->save()) {
            return array('error'=>'操作失败');
        }else{
            return array('error'=>'');
        }
    }


}
